﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ZhongJia.Web.Framework.Startup;
using GanQL.Web.Infrastructure.Infrastructure;
using GuanQL.Service;
using Microsoft.Extensions.Options;
using GanQL.Web.Infrastructure;
using GanQL.Web.Infrastructure.Filters;
using GanQL.Web.Infrastructure.Swaggers;
using Microsoft.Extensions.PlatformAbstractions;
using Swashbuckle.Swagger.Model;

namespace GanQL.Web
{
    public class Startup : WebApiStartup
    {


        public Startup(IHostingEnvironment env) : base(env)
        {
            var builder = new WebApiStartupBuilder().UseSwaggerUi(options =>
            {
                options.SingleApiVersion(new Info
                {
                    Version = "v1",
                    Title = "- api",
                });
                options.OperationFilter<GlobalParameterOperationFilter>();
                options.OperationFilter<AuthorizationHeaderParameterOperationFilter>();
                options.IncludeXmlComments(PlatformServices.Default.Application.ApplicationBasePath + "\\GuanQL.Web.xml");
            });
            StartupOption = builder.Build();
            StartupOption.MVCFilters.Add(typeof(AuditFilter));
            StartupOption.MVCFilters.Add(typeof(LogUserOperTimeFilter));
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            ConfigureBaseServices(services);
            services.AddCors(options =>
            {
                options.AddPolicy("AllowSpecificOrigin", builder => builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
            });
            services.AddEfCore(Environment, Configuration);
            services.AddCustomizedIdentity();
            services.ConfigureOptions(Configuration);
           // services.AddSingleton<TcpServer>();
            Bootstrapper.RegisterServices(services, Configuration);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseAuth(app.ApplicationServices.GetService<IOptions<TokenProviderOptions>>().Value);
          //  app.ApplicationServices.GetService<TcpServer>().Start();
            app.UseCors("AllowSpecificOrigin");
            if (env.IsDevelopment())
            {
                ConfigureBaseDevelopment(app, env, loggerFactory);
            }
            else
            {
                ConfigureBaseProduction(app, env, loggerFactory);
            }
        }
        //public Startup(IHostingEnvironment env)
        //{
        //    var builder = new ConfigurationBuilder()
        //        .SetBasePath(env.ContentRootPath)
        //        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        //        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        //        .AddEnvironmentVariables();
        //    Configuration = builder.Build();
        //}

        //public IConfigurationRoot Configuration { get; }

        //// This method gets called by the runtime. Use this method to add services to the container.
        //public void ConfigureServices(IServiceCollection services)
        //{
        //    // Add framework services.
        //    services.AddMvc();
        //}

        //// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        //public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        //{
        //    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        //    loggerFactory.AddDebug();

        //    app.UseMvc();
        //}
    }
}
